package com.traffic.api.restful;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;

import org.apache.log4j.Logger;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.google.gson.Gson;
import com.gxhl.api.core.model.GDPResponse;
import com.traffic.api.config.ApiProperties;
import com.traffic.api.exception.ApiBizException;
import com.traffic.api.jedis.JedisClient;
import com.traffic.managent.service.order.bean.OrderBean;

@Path("/{v}/order")
public class OrderRestful extends CheckRestful {
	private static Logger LOG = Logger.getLogger(OrderRestful.class);
	static JedisClient client = JedisClient.getInstance();
	@Context
	private HttpServletRequest httpRequest;

	@GET
	@Produces(MediaType.TEXT_PLAIN)
	@Path("/test")
	public String getResult(@QueryParam("name") String name) {
		return name;
	}

	@POST
	@Consumes({ "application/json" })
	@Produces({ "application/json" })
	public GDPResponse order(JSONObject param, @PathParam("v") String v) {
		LOG.info("api- order params :" + param.toString());
		GDPResponse result = new GDPResponse();
		try {
			/**
			 * 验证类 1成功，则返回订单的信息 2失败，则try
			 */
			Map<String, Object> params = check(param, "order");

			result.setCode("0000");
			result.setMsg("成功");
			result.setData(params);
		} catch (ApiBizException e) {
			LOG.error(e.getMessage(),e);
			result.setCode("0001");
			result.setMsg(e.getCause().getMessage());
		} catch (Exception e) {
			LOG.error(e.getMessage(),e);
			result.setCode("0001");
			result.setMsg("(api)系统异常");
			return result;
		}
		LOG.info("api- order result :" + new Gson().toJson(result));
		return result;
	}

	@POST
	@Consumes({ "application/json" })
	@Produces({ "application/json" })
	@Path("/status")
	public GDPResponse getStatus(JSONObject param, @PathParam("v") String v) {
		LOG.info("api- getStatus params :" + param.toString());
		GDPResponse result = new GDPResponse();
		try {
			Map<String, Object> params = check(param, "status");

			String cstmOrderNo = String.valueOf(params.get("cstmOrderNo"));

			if ((cstmOrderNo == null) || ("".equals(cstmOrderNo))) {
				result.setCode("0001");
				result.setMsg("流水号不能为空");
				return result;
			}
			/**
			 * 先从 result里面查询，在goods，在accept
			 */
			OrderBean orderBean = null;
			if (orderBean == null) {
				orderBean = client.get("orderno_" + cstmOrderNo + "result");
			}
			if (orderBean == null) {
				orderBean = client.get("orderno_" + cstmOrderNo + "goods");
			}
			if (orderBean == null) {
				orderBean = client.get("orderno_" + cstmOrderNo + "accept");
			}
			if (orderBean == null) {
				result.setCode("0001");
				result.setMsg("未找到相应订单信息");
			} else {
				HashMap<String, Object> data = new HashMap<String, Object>();
				data.put("orderNo", orderBean.getOrderNo());
				data.put("cstmOrderNo", orderBean.getCstmOrderNo());
				data.put("status", orderBean.getStatus());
				data.put("errorDesc", orderBean.getErrorMsg());

				result.setCode("0000");
				result.setMsg(orderBean.getMessage());
				result.setData(data);

			}
		} catch (ApiBizException e) {
			LOG.error(e.getMessage(),e);
			result.setCode("0001");
			result.setMsg(e.getCause().getMessage());
		} catch (Exception e) {
			LOG.error(e.getMessage(),e);
			result.setCode("0001");
			result.setMsg("系统异常(apis),稍后查询");
			LOG.info("api- status params :" + param.toString() + "系统异常" + e);
		}
		LOG.info("api- rest/1.0/order/status result :" + new Gson().toJson(result));
		return result;
	}

	//===============================================以下的下游是爸爸  ===============================================================
   /**
	 * 
	 * 万恒专用
	*/
	@POST
	@Consumes({ "application/json" })
	@Produces({ "application/json;charset=utf-8"})
	@Path("/recharge")
	public String recharge(JSONObject param, @PathParam("v") String v) {
		LOG.info("万恒订购api- recharge params :" + param.toString());
		Gson gson = new Gson();
		String result = "";
		Map<String, String> returnMap = new HashMap<String, String>();
		try {

			String token = param.getString("token");
			if (ApiProperties.WANHENG_TOKEN.equals(token)) {
				result = checkValidParam(param, gson, result, returnMap);
				if (!"".equals(result)) {
					return result;
				}
				JSONObject params = new JSONObject();
				params.put("appkey",ApiProperties.WANHENG_KEY);
				params.put("phoneNo", param.getString("mobile"));
				params.put("productId", param.getString("package"));
				params.put("cstmOrderNo", param.getString("orderId"));
				params.put("appkey",ApiProperties.WANHENG_KEY);
				params.put("callbackUrl",ApiProperties.WANHENG_CALLBACK_URL);
				params.put("range", param.getString("range"));//流量类型(签名) 0漫游流量  1省内流量 2 红包流量
				params.put("alleyway", param.getString("alleyway"));//0全国，1省份
				Map<String, Object> resultMap = checkParam(params,"order");
				returnMap.put("orderId",(String)resultMap.get("cstmOrderNo"));
				returnMap.put("resultCode", "200");
				returnMap.put("resultMsg", "提交成功");
				result = gson.toJson(returnMap);
			}else {
				returnMap.put("orderId", param.get("orderId").toString());
				returnMap.put("resultCode", "406");
				returnMap.put("resultMsg", "token失效");
				result = gson.toJson(returnMap);
			}	
		
		}catch (ApiBizException e) {
			LOG.error(e.getMessage(),e);
			returnMap.put("resultCode", "500");
			returnMap.put("resultMsg", e.getCause().getMessage());
			result = gson.toJson(returnMap);
		} catch (Exception e) {
			LOG.error(e.getMessage(),e);
			returnMap.put("resultCode", "500");
			returnMap.put("resultMsg", e.getCause().getMessage());
			result = gson.toJson(returnMap);

		}
		LOG.info("api- recharge返回参数 :" + result);	
		return result;
	}

	@POST
	@Consumes({ "application/json" })
	@Produces({ "application/json;charset=utf-8" })
	@Path("/query")
	public String query(JSONObject param, @PathParam("v") String v) {
		LOG.info("api- query params :" + param.toString());
		Gson gson = new Gson();
		String result = "";
		Map<String, String> returnMap = new HashMap<String, String>();
	
		try {
			String cstmOrderNo = param.getString("orderId");
			 if (null==cstmOrderNo|| "".equals(cstmOrderNo)) {
				    returnMap.put("resultCode","500");
				    returnMap.put("resultMsg","流水号不能为空");
				    result = gson.toJson(returnMap);
				    return result;
				}
			 String token = param.getString("token");
			if (ApiProperties.WANHENG_TOKEN.equals(token)) {
				JSONObject params = new JSONObject();
				params.put("appkey",ApiProperties.WANHENG_KEY);
				checkParam(params, "status");
				/**
				 * 先从 result里面查询，在goods，在accept
				 */
				OrderBean orderBean = null;
				if (orderBean == null) {
					orderBean = client.get("orderno_" + cstmOrderNo + "result");
				}
				if (orderBean == null) {
					orderBean = client.get("orderno_" + cstmOrderNo + "goods");
				}
				if (orderBean == null) {
					orderBean = client.get("orderno_" + cstmOrderNo + "accept");
				}
				if (orderBean == null) {
					returnMap.put("orderId",cstmOrderNo);
					returnMap.put("resultCode", "203");
					returnMap.put("resultMsg","未找到相应订单信息");
					result = gson.toJson(returnMap);
				} else {
					String status = orderBean.getStatus();
					if ("0".equals(status) || "1".equals(status)) {
						returnMap.put("orderId",cstmOrderNo);
						returnMap.put("resultCode", "202");
						returnMap.put("resultMsg","订单受理中");
						returnMap.put("mobile",orderBean.getPhoneNo());//package
						returnMap.put("package",String.valueOf(orderBean.getProductSize()));
						String sign = MD5W("orderId="+cstmOrderNo+"&resultCode=202&mobile="+orderBean.getPhoneNo()+"&package="+String.valueOf(orderBean.getProductSize())+"&key="+ApiProperties.WANHENG_SECURITY);
						returnMap.put("sign",sign);
						result = gson.toJson(returnMap);
					} else if ("7".equals(status)) {
						returnMap.put("orderId",cstmOrderNo);
						returnMap.put("resultCode","200");
						returnMap.put("resultMsg","订购成功");
						returnMap.put("mobile",orderBean.getPhoneNo());
						returnMap.put("package",String.valueOf(orderBean.getProductSize()));
						String sign = MD5W("orderId="+cstmOrderNo+"&resultCode=200&mobile="+orderBean.getPhoneNo()+"&package="+String.valueOf(orderBean.getProductSize())+"&key="+ApiProperties.WANHENG_SECURITY);
						returnMap.put("sign",sign);
						result = gson.toJson(returnMap);
					} else {// 8
						returnMap.put("orderId",cstmOrderNo);
						returnMap.put("resultCode", "100");
						returnMap.put("resultMsg",orderBean.getErrorMsg());
						returnMap.put("mobile",orderBean.getPhoneNo());//package
						returnMap.put("package",String.valueOf(orderBean.getProductSize()));
						String sign = MD5W("orderId="+cstmOrderNo+"&resultCode=100&mobile="+orderBean.getPhoneNo()+"&package="+String.valueOf(orderBean.getProductSize())+"&key="+ApiProperties.WANHENG_SECURITY);
						returnMap.put("sign",sign);
						result = gson.toJson(returnMap);
					}
				
				}
			}else {
				returnMap.put("orderId", param.get("orderId").toString());
				returnMap.put("resultCode", "406");
				returnMap.put("resultMsg", "token失效");
				result = gson.toJson(returnMap);
			}
			
		} catch (ApiBizException e) {
			LOG.error(e.getMessage(),e);
			returnMap.put("resultCode", "500");
			returnMap.put("resultMsg", e.getMessage());
			result = gson.toJson(returnMap);

		} catch (Exception e) {
			LOG.error(e.getMessage(),e);
			
			returnMap.put("resultCode", "500");
			returnMap.put("resultMsg", e.getMessage());
			result = gson.toJson(returnMap);
		}
		LOG.info("api- query返回参数 :" + result);	
		return result;
	}
	
	public static String MD5W(String message) {
		System.out.println("getMD5STR " + message);
		MessageDigest messageDigest = null;
		StringBuffer md5StrBuff = new StringBuffer();
		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update(message.getBytes("UTF-8"));

			byte[] byteArray = messageDigest.digest();
			for (int i = 0; i < byteArray.length; i++) {
				if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
					md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
				else
					md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
			}
		} catch (Exception e) {
			throw new RuntimeException();
		}
		System.out.println("getMD5====>" + md5StrBuff.toString().toLowerCase());
		return md5StrBuff.toString().toLowerCase();// 字母小写
	}
	
	//万恒校验参数
	private String checkValidParam(JSONObject param, Gson gson, String result,Map<String, String> returnMap) throws JSONException {
		if ((param.getString("mobile") == null) || ("".equals(param.getString("mobile")))) {
			returnMap.put("resultCode","500");
			returnMap.put("resultMsg","手机号码不能为空");
			
			result = gson.toJson(returnMap);
		}
		if (isMobileNO(param.getString("mobile"))) {
			returnMap.put("resultCode","403");
			returnMap.put("resultMsg","手机格式异常");
			
			result = gson.toJson(returnMap);
		}
		if ((param.getString("package") == null) || ("".equals(param.getString("package")))) {

			returnMap.put("resultCode","500");
			returnMap.put("resultMsg","流量包大小不能为空");
			result = gson.toJson(returnMap);
		}
		if ((param.getString("range") == null) || ("".equals(param.getString("range")))) {

			returnMap.put("resultCode","500");
			returnMap.put("resultMsg","流量类型不能为空");
			result = gson.toJson(returnMap);
		}
		if ((param.getString("orderId") == null) || ("".equals(param.getString("orderId")))) {
			returnMap.put("resultCode","500");
			returnMap.put("resultMsg","流水号不能为空");
			result = gson.toJson(returnMap);
		}
		// 我们只在万恒平台配置了 0 1 6 三种类型的包型
		if ((!"0".equals(param.getString("range")))&&(!"1".equals(param.getString("range")))&&(!"6".equals(param.getString("range")))) {

			returnMap.put("resultCode","500");
			returnMap.put("resultMsg","流量类型未配置");
			result = gson.toJson(returnMap);
		}
	   return result;
	}
	
	
	@POST
	@Consumes({"application/x-www-form-urlencoded"})  
	@Produces({"text/plain;charset=gbk"})
	@Path("/db/recharge")
	public String recharge_db(MultivaluedMap<String, String> formParams,@PathParam("v") String v) {
		LOG.info("api- rest/1.0/recharge_db 请求参数： :" + formParams);
		StringBuilder resultStringBuilder = new StringBuilder();
		String INTECMD = formParams.getFirst("INTECMD");//接口命令字
		//String SIGN = formParams.getFirst("SIGN");//签名内容
		String CTMID = formParams.getFirst("CTMID");//商户编号
		String CTMORDID = formParams.getFirst("CTMORDID");//商户交易流水号
		//String CTMTIME= formParams.getFirst("CTMTIME");//商户请求交易时间
		String PLAYACC = formParams.getFirst("PLAYACC");//手机号码
		String PDTVALUE = formParams.getFirst("PDTVALUE");//充值面额
		String CTMRETURL = formParams.getFirst("CTMRETURL");//订单结果回调地址
		//String URPROVID = formParams.getFirst("URPROVID");//用户省份
		//String ISP = formParams.getFirst("ISP");//运营商
		String ROAMING_TYPE = formParams.getFirst("ROAMING_TYPE");//漫游类型  0 - 省内漫游（归属省份可用） 1 - 全国漫游（全国可用）
		//String TRAFFIC_TYPE = formParams.getFirst("TRAFFIC_TYPE");//流量来源
		String EFFECTIVE_ID = formParams.getFirst("EFFECTIVE_ID");//时间有效性
		
		String result = "";

		result = checkValidOrder_Db(resultStringBuilder, INTECMD, CTMID,CTMORDID, PLAYACC, PDTVALUE, ROAMING_TYPE, EFFECTIVE_ID, result);
		if (!"".equals(result)) {
			return result;
		}
		try {
			JSONObject params = new JSONObject();
			params.put("appkey", ApiProperties.DIANBO_CTMID);//商户ID
			params.put("phoneNo", PLAYACC);
			params.put("productId", PDTVALUE);
			params.put("cstmOrderNo",CTMORDID);
			params.put("callbackUrl",CTMRETURL);
			params.put("roaming_type",ROAMING_TYPE);//0全国，1省份
			params.put("effective_id",EFFECTIVE_ID);
			Map<String, Object> resultMap = checkParam(params,"order");
			String cstmOrderNo =(String)resultMap.get("cstmOrderNo");
			String orderNo = (String)resultMap.get("orderNo");
			String orderTime = (String)resultMap.get("orderTime");
			String waitSign ="CTMID="+CTMID+"&CTMORDID="+cstmOrderNo+"&STATUS=1"+"&TRDACTVALUE="+PDTVALUE+"&TRDREQTIME="+orderTime+"&TRDSN="+orderNo+ApiProperties.DIANBO_KEY;
			String sign = getMD5(waitSign);
		
			resultStringBuilder.append("SIGN="+sign).append("STATUS=1").append("&CTMID="+CTMID).append("&CTMORDID="+cstmOrderNo).append("&TRDSN="+orderNo).append("&TRDREQTIME="+orderTime).append("&TRDACTVALUE="+PDTVALUE);
		
		}catch (ApiBizException e) {
			LOG.error(e.getMessage(),e);
			if (e.getCause().getMessage().contains("(api)您的ip未绑定")) {
				String waitSign ="CTMID="+CTMID+"&CTMORDID="+CTMORDID+"&STATUS=111"+ApiProperties.DIANBO_KEY;
				String sign = getMD5(waitSign);
				result = resultStringBuilder.append("STATUS=111").append("&CTMID="+CTMID).append("&CTMORDID="+CTMORDID).append("&SIGN="+sign).toString();
			}else if (e.getCause().getMessage().contains("重复的订单号")) {
				String waitSign ="CTMID="+CTMID+"&CTMORDID="+CTMORDID+"&STATUS=219"+ApiProperties.DIANBO_KEY;
				String sign = getMD5(waitSign);
				result = resultStringBuilder.append("STATUS=219").append("&CTMID="+CTMID).append("&CTMORDID="+CTMORDID).append("&SIGN="+sign).toString();
			}else{
				result = resultStringBuilder.append("STATUS=109").append("&CTMID="+CTMID).append("&CTMORDID="+CTMORDID).toString();
			}
		} catch (Exception e) {
			LOG.error(e.getMessage(),e);
			String waitSign ="CTMID="+CTMID+"&CTMORDID="+CTMORDID+"&STATUS=999"+ApiProperties.DIANBO_KEY;
			String sign = getMD5(waitSign);
			result = resultStringBuilder.append("STATUS=999").append("&CTMID="+CTMID).append("&CTMORDID="+CTMORDID).append("&SIGN="+sign).toString();

		}
		LOG.info("api- recharge_db返回参数 :" + result);	
		return result;
	}

	private String checkValidOrder_Db(StringBuilder resultStringBuilder,String INTECMD, String CTMID, String CTMORDID, String PLAYACC,String PDTVALUE, String ROAMING_TYPE, String EFFECTIVE_ID,String result) {
		if (!"LLCZ".equals(INTECMD)) {//接口命令字
			result = resultStringBuilder.append("STATUS=106").append("&CTMID="+CTMID).append("&CTMORDID="+CTMORDID).toString();
		}
		//109 商户确认
		if (!ApiProperties.DIANBO_CTMID.equals(CTMID)) {//商户确认
			result =  resultStringBuilder.append("STATUS=109").append("&CTMID="+CTMID).append("&CTMORDID="+CTMORDID).toString();
		}
		if (!"0".equals(EFFECTIVE_ID)&&!"1".equals(EFFECTIVE_ID)) {//月包和日包
			String waitSign ="CTMID="+CTMID+"&CTMORDID="+CTMORDID+"&STATUS=103"+ApiProperties.DIANBO_KEY;
			String sign = getMD5(waitSign);
			result =  resultStringBuilder.append("STATUS=103").append("&CTMID="+CTMID).append("&CTMORDID="+CTMORDID).append("&SIGN="+sign).toString();
		}
		
		if ((PLAYACC == null) || ("".equals(PLAYACC))) {
			result =  resultStringBuilder.append("STATUS=104").append("&CTMID="+CTMID).append("&CTMORDID="+CTMORDID).toString();
		}
		if ((PDTVALUE == null) || ("".equals(PDTVALUE))) {
			result =  resultStringBuilder.append("STATUS=104").append("&CTMID="+CTMID).append("&CTMORDID="+CTMORDID).toString();
		}
		if ((ROAMING_TYPE == null) || ("".equals(ROAMING_TYPE))) {
			result =  resultStringBuilder.append("STATUS=104").append("&CTMID="+CTMID).append("&CTMORDID="+CTMORDID).toString();
		}
		if ((CTMORDID == null) || ("".equals(CTMORDID))) {
			result =  resultStringBuilder.append("STATUS=104").append("&CTMID="+CTMID).append("&CTMORDID="+CTMORDID).toString();
		}
		return result;
	}

	/**
	 * 点博专用
	 * @param formParams
	 * @return
	 */
	@POST
	@Consumes({"application/x-www-form-urlencoded"})  
	@Produces({"text/plain;charset=gbk"})
	@Path("/db/query")
	public String query_db(MultivaluedMap<String, String> formParams,@PathParam("v") String v) {
		LOG.info("api- rest/1.0/query_db 请求参数： :" + formParams);
		StringBuilder resultStringBuilder = new StringBuilder();
		String INTECMD = formParams.getFirst("INTECMD");//接口命令字
		//String SIGN = formParams.getFirst("SIGN");//签名内容
		String CTMID = formParams.getFirst("CTMID");//商户编号
		String CTMORDID = formParams.getFirst("CTMORDID");//商户交易流水号
		String cstmOrderNo = CTMORDID;
		String result = "";
		if (!"DDCX".equals(INTECMD)) {
			result = resultStringBuilder.append("STATUS=106").append("&CTMID="+CTMID).append("&CTMORDID="+CTMORDID).toString();
			return result;
		}
		
		if (!ApiProperties.DIANBO_CTMID.equals(CTMID)) {// 商户确认
			result = resultStringBuilder.append("STATUS=109").append("&CTMID=" + CTMID).append("&CTMORDID=" + CTMORDID).toString();
			return result;
		}
		try {
			
			JSONObject param = new JSONObject();
			param.put("appkey", ApiProperties.DIANBO_CTMID);
			checkParam(param,"status");
			
			/**
			 * 先从 result里面查询，在goods，在accept
			 */
			OrderBean orderBean = null;
			if (orderBean == null) {
				orderBean = client.get("orderno_" + cstmOrderNo + "result");
			}
			if (orderBean == null) {
				orderBean = client.get("orderno_" + cstmOrderNo + "goods");
			}
			if (orderBean == null) {
				orderBean = client.get("orderno_" + cstmOrderNo + "accept");
			}
			if (orderBean == null) {
				String waitSign ="CTMID="+CTMID+"&CTMORDID="+CTMORDID+"&TRADESTATUS=101"+ApiProperties.DIANBO_KEY;
				String sign = getMD5(waitSign);
				result = resultStringBuilder.append("SIGN="+sign).append("&TRADESTATUS=101").append("&CTMID="+CTMID).append("&CTMORDID"+CTMORDID).toString();
			
			} else {
				String status = orderBean.getStatus();
				if ("0".equals(status) || "1".equals(status)) {	
					String waitSign ="CTMID="+CTMID+"&CTMORDID="+CTMORDID+"&TRADESTATUS=2"+ApiProperties.DIANBO_KEY;
					String sign = getMD5(waitSign);
					resultStringBuilder.append("SIGN="+sign).append("&TRADESTATUS=2").append("&CTMID="+CTMID).append("&CTMORDID="+CTMORDID);
					
				} else if ("7".equals(status)) {

					String orderNo = orderBean.getOrderNo();
					String orderTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(orderBean.getOrderTime());
					String openTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(orderBean.getOpenTime());
					String productId = String.valueOf(orderBean.getProductSize());
				    
					String waitSign ="CTMID="+CTMID+"&CTMORDID="+CTMORDID+"&TRADESTATUS=2"+"&TRDACTVALUE="+productId+"&TRDFINTIME="+openTime+"&TRDREQTIME="+orderTime+"&TRDSN="+orderNo+ApiProperties.DIANBO_KEY;
					String sign = getMD5(waitSign);
					resultStringBuilder.append("SIGN="+sign).append("&TRADESTATUS=1").append("&CTMID="+CTMID).append("&CTMORDID"+CTMORDID).append("&TRDSN="+orderNo).append("&TRDREQTIME="+orderTime).append("&TRDFINTIME="+openTime).append("&TRDACTVALUE="+productId);
				} else {// 8
					String orderNo = orderBean.getOrderNo();
					String orderTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(orderBean.getOrderTime());
					String openTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(orderBean.getOpenTime());
					String productId = String.valueOf(orderBean.getProductSize());
					String waitSign ="CTMID="+CTMID+"&CTMORDID="+CTMORDID+"&TRADEERROR=996"+"&TRADESTATUS=2"+"&TRDACTVALUE="+productId+"&TRDFINTIME="+openTime+"&TRDREQTIME="+orderTime+"&TRDSN="+orderNo+ApiProperties.DIANBO_KEY;
					String sign = getMD5(waitSign);
					resultStringBuilder.append("SIGN="+sign).append("&TRADESTATUS=0").append("&CTMID="+CTMID).append("CTMORDID"+CTMID).append("&TRDSN="+orderNo).append("&TRDREQTIME="+orderTime).append("&TRDFINTIME="+openTime).append("&TRDACTVALUE="+productId).append("&TRADEERROR=996");
					
				}
			}

		
		} catch (ApiBizException e) {
			LOG.error(e.getMessage(),e);
			if (e.getCause().getMessage().contains("(api)您的ip未绑定")) {
				String waitSign ="CTMID="+CTMID+"&CTMORDID="+CTMORDID+"&TRADESTATUS=111"+ApiProperties.DIANBO_KEY;
				String sign = getMD5(waitSign);
				result = resultStringBuilder.append("TRADESTATUS=111").append("&CTMID="+CTMID).append("&CTMORDID="+CTMORDID).append("&SIGN="+sign).toString();
			}else {
				result = resultStringBuilder.append("TRADESTATUS=109").append("&CTMID="+CTMID).append("&CTMORDID="+CTMORDID).toString();
			}
		} catch (Exception e) {
			LOG.error(e.getMessage(),e);
			
		}
		LOG.info("api- query_db返回参数 :" + result);	
		return result;
	}
	
	public static String getMD5(String message) {
		System.out.println("getMD5STR " + message);
		MessageDigest messageDigest = null;
		StringBuffer md5StrBuff = new StringBuffer();
		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update(message.getBytes("GBK"));

			byte[] byteArray = messageDigest.digest();
			for (int i = 0; i < byteArray.length; i++) {
				if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
					md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
				else
					md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
			}
		} catch (Exception e) {
			throw new RuntimeException();
		}
		System.out.println("getMD5====>" + md5StrBuff.toString().toLowerCase());
		return md5StrBuff.toString().toLowerCase();// 字母小写
	}			


	
	public static boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9])|(17[0-9]))\\d{8}$");
		Matcher m = p.matcher(mobiles);
		return (!(m.matches()));
	}

}